/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.sys.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.sys.entity.Config;
import com.boxin.ims.modules.sys.dao.ConfigDao;

/**
 * 全局配置Service
 * @author Jakemanse
 * @version 2013-07-31
 */
@Component
@Transactional(readOnly = true)
public class ConfigService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ConfigService.class);
	
	@Autowired
	private ConfigDao configDao;
	
	public Config get(Long id) {
		return configDao.findOne(id);
	}
	
	public Page<Config> find(Page<Config> page, Config config) {
		DetachedCriteria dc = configDao.createDetachedCriteria();
		if (config.getUser()!=null && config.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", config.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(config.getName())){
			dc.add(Restrictions.like("name", "%"+config.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(config.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+config.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", Config.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return configDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(Config config) {
		configDao.save(config);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		configDao.deleteById(id);
	}
	
}
